﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AddDevices : System.Web.UI.Page
{
    public ProxyWrapper.UserProxy.UserDetails UserObject { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            if (Session["userObject"] == null)
            {
                Response.Redirect("Default.aspx", false);
            }
            else
            {
                UserObject = ((ProxyWrapper.UserProxy.UserDetails)Session["UserObject"]);

                ProxyWrapper.UserServiceWrapper _usr = new ProxyWrapper.UserServiceWrapper();
                DataSet ds = _usr.GetAccountsDevices(UserObject.UserId, UserObject.HashCode);

                DataTable Data = new DataTable();
                if (Request.Form["query"] != null)
                {
                    string search = Request.Form["query"].ToString();
                    DataTable dtFiltered = ds.Tables[0].AsEnumerable().Where(m => m.Field<string>("AppUserName").Contains(search) || m.Field<string>("DeviceName").Contains(search)).OrderByDescending(m => m.Field<int>("LastLogin")).CopyToDataTable();
                    lstDevices.DataSource = dtFiltered;
                    lstDevices.DataBind();
                }
                else
                {
                    lstDevices.DataSource = ds;
                    lstDevices.DataBind();
                }

            }
        }
        catch (Exception ex)
        { }
    }
}